package com.leon.androidcoursedesign.utils.dataBaseUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.annotation.Nullable;

import com.leon.androidcoursedesign.beans.Course;
import com.leon.androidcoursedesign.beans.Note;

import java.util.ArrayList;
import java.util.List;

public class NoteUtils extends SQLiteHelper {
    private final SQLiteHelper sqLiteHelper;
    private final SQLiteDatabase sqLiteDatabase;
    private final String dbName = Tables.Note.value;

    public NoteUtils(@Nullable Context context) {
        super(context, 1);

        this.sqLiteHelper = new SQLiteHelper(context, 1);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    }

    /**
     * 查询所有未删除的note
     *
     * @return  返回所有note
     */
    public List<Note> queryAllNote(String operationId) {
        if (operationId != null) {
            Cursor cursor = sqLiteDatabase.query(dbName, null, "isDelete = ? AND operationId = ?", new String[]{"false", operationId}, null, null, null);

            List<Note> notes = new ArrayList<>();
            while (cursor.moveToNext()) {
                Note note = new Note();

                note.id = cursor.getInt(cursor.getColumnIndex("id"));
                note.itemId = cursor.getString(cursor.getColumnIndex("itemId"));
                note.operationId = cursor.getString(cursor.getColumnIndex("operationId"));
                note.noteId = cursor.getString(cursor.getColumnIndex("noteId"));
                note.showName = cursor.getString(cursor.getColumnIndex("showName"));
                note.saveTime = cursor.getLong(cursor.getColumnIndex("saveTime"));

                notes.add(note);
            }
            cursor.close();

            return notes;
        }

        return null;

    }

    /**
     * 查询指定note
     *
     * @param noteId    noteId
     * @return  返回note存在状态
     */
    public boolean queryNote(String noteId, String operationId) {
        Cursor cursor = sqLiteDatabase.query(dbName, null, "noteId = ? AND isDelete = ? AND operationId = ?", new String[]{noteId, "false", operationId}, null, null, null);

        int count = cursor.getCount();
        cursor.close();

        return count > 0;
    }

    /**
     * 将note添加至‘Note’
     *
     * @param note    course对象
     * @return  返回插入状态
     */
    public boolean addNote(Note note) {
        ContentValues contentValues = new ContentValues();

        contentValues.put("noteId", note.noteId);
        contentValues.put("itemId", note.itemId);
        contentValues.put("operationId", note.operationId);
        contentValues.put("showName", note.showName);
        contentValues.put("saveTime", note.saveTime);

        long insert = sqLiteDatabase.insert(dbName, null, contentValues);

        return insert > 0;
    }

    /**
     * 根据noteId删除对应note
     *
     * @param noteId    noteId
     * @return  返回删除状态
     */
    public boolean deleteNote(String itemId, String noteId, String operationId) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("isDelete", true);

        int state = sqLiteDatabase.update(dbName, contentValues, "itemId = ? AND noteId = ? AND operationId = ?", new String[]{itemId, noteId, operationId});

        return  state > 0;
    }

    /**
     * 更新指定note的数据
     *
     * @param note    新的note数据
     * @return  返回存在状态
     */
    public boolean updateNote(Note note) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("showName", note.showName);
        contentValues.put("saveTime", note.saveTime);

        int state = sqLiteDatabase.update(dbName, contentValues, "noteId = ? AND operationId = ?", new String[]{note.noteId, note.operationId});

        return  state > 0;
    }
}
